package com.study.service.communication;

import feign.Logger;
import feign.Request;
import feign.RetryableException;
import feign.Retryer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * OpenFeign 配置类
 */
@Configuration
public class FeignConfig {

    /**
     * 开启openFeign 日志
     * @return
     */
    @Bean
    public Logger.Level feignLogger(){
        return Logger.Level.FULL; //需要注意、日志级别需要修改成 debug级别
    }

    /**
     * OpenFeign 开启重试
     * period = 100 发起当前请求时间隔 ms单位
     * maxPeriod = 1000 发起当前请求最大时间隔 ms单位
     * maxAttempts 最多重试次数
     */
    @Bean
    public Retryer feignRetryer(){
        return new Retryer.Default(
                100,
                SECONDS.toMillis(1),
                5
        );
    }

    public static final int CONNECT_TIMEOUT_MILLS = 5000;
    public static final int READ_TIMEOUT_MILLS = 5000;

    //对请求连接和响应时间进行限制
    @Bean
    public Request.Options options(){
        return new Request.Options(
                CONNECT_TIMEOUT_MILLS, TimeUnit.MICROSECONDS,
                READ_TIMEOUT_MILLS,TimeUnit.MILLISECONDS,
                true
        );
    }
}
